﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FinalFantasy6Randomizer
{
    public delegate void ColorChanged(object sender);

    public partial class ColorPicker : UserControl
    {
        public event ColorChanged RedChanged;
        public event ColorChanged GreenChanged;
        public event ColorChanged BlueChanged;

        private int red;
        private int green;
        private int blue;

        private const decimal colorConversion = 8.225806m;


        public int Red
        {
            get { return red; }
            set
            {
                red = value;
                pickRed.Value = red;

                if (RedChanged != null)
                    RedChanged(this);
            }
        }

        public int Green
        {
            get { return green; }
            set
            {
                green = value;
                pickGreen.Value = green;

                if (GreenChanged != null)
                    GreenChanged(this);
            }
        }

        public int Blue
        {
            get { return blue; }
            set
            {
                blue = value;
                pickBlue.Value = blue;

                if (BlueChanged != null)
                    BlueChanged(this);
            }
        }

        public Color RgbColor
        {
            get
            {
                var newRed = (int)Math.Round(pickRed.Value * colorConversion);
                var newGreen = (int)Math.Round(pickGreen.Value * colorConversion);
                var newBlue = (int)Math.Round(pickBlue.Value * colorConversion);

                return Color.FromArgb(newRed, newGreen, newBlue);
            }
        }

        public ColorPicker()
        {
            InitializeComponent();
        }

        private void RefreshColor()
        {
            var newRed = (int)Math.Round(pickRed.Value * colorConversion);
            var newGreen = (int)Math.Round(pickGreen.Value * colorConversion);
            var newBlue = (int)Math.Round(pickBlue.Value * colorConversion);

            colorPreview.BackColor = Color.FromArgb(newRed, newGreen, newBlue);
        }

        private void pickRed_ValueChanged(object sender, EventArgs e)
        {
            Red = (int)pickRed.Value;
            RefreshColor();
        }

        private void pickGreen_ValueChanged(object sender, EventArgs e)
        {
            Green = (int)pickGreen.Value;
            RefreshColor();
        }

        private void pickBlue_ValueChanged(object sender, EventArgs e)
        {
            Blue = (int)pickBlue.Value;
            RefreshColor();
        }

    }
}
